package exercise;

import java.util.PriorityQueue;

// https://leetcode-cn.com/problems/kth-largest-element-in-an-array/
public class L230702_I {
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(k);
        for (int i = 0; i < k; i++) {
            priorityQueue.offer(nums[i]);
        }

        for (int i = k; i < nums.length; i++) {
            int x = priorityQueue.peek();
            if (x < nums[i]) {
                priorityQueue.poll();
                priorityQueue.offer(nums[i]);
            }
        }
        return priorityQueue.peek();
    }

    public static void main(String[] args) {
        int[] arr = {3,2,3,1,2,4,5,5,6};
        L230702_I s = new L230702_I();
        System.out.println(s.findKthLargest(arr, 4));
    }
}
